package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.POJO.ReceiveOrder;
import com.itheima.POJO.ShunDai;
import com.itheima.service.IReceiveOrderService;
import com.itheima.service.IShunDaiService;
import com.itheima.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zqystart
 * @create 2022-09-01-11:26
 */
@RestController
@RequestMapping("/receiveorder")
public class ReceiveOrderController {
    @Autowired
    private IShunDaiService shunDaiService;

    @Autowired
    private IReceiveOrderService receiveOrderService;


    @GetMapping("/save")
    public R save(int id){//保存接单
        ShunDai shunDai = shunDaiService.getById(id);
        ReceiveOrder receiveOrder=new ReceiveOrder();
        receiveOrder.setShunDai(shunDai);
        receiveOrder.setShunDaiUsername(shunDai.getUsername());
        receiveOrder.setShunDaiMoney(shunDai.getMoney());
        receiveOrder.setShunDaiReceiveAddress(shunDai.getReceiveAddress());
        receiveOrder.setShunDaiSend2Address(shunDai.getSend2Address());
        receiveOrder.setShunDaiRequest(shunDai.getRequest());
        receiveOrder.setShunDaiTel(shunDai.getTel());
        receiveOrder.setOrderNo(StringUtil.produce());
        boolean save = receiveOrderService.save(receiveOrder);
        Map<String,Object> map=new HashMap<>();
        map.put("message",save);
        return R.ok(map);
    }

//    @GetMapping("/selectAll")
//    public R selectAll(){//查询所有的接单
//        List<ReceiveOrder> list = receiveOrderService.list();
//        for (ReceiveOrder receiveOrder : list) {
//            ShunDai shunDai = shunDaiService.getById(receiveOrder.getShunDaiId());
//            receiveOrder.setShunDai(shunDai);
//        }
//        Map<String,Object> map=new HashMap<>();
//        map.put("message",list);
//        return R.ok(map);
//    }

    @GetMapping("/selectByUsername")
    public R selectByUsername(String username){//通过用户名查询所有的接单
        List<ReceiveOrder> list = receiveOrderService.list(new QueryWrapper<ReceiveOrder>().eq("shunDaiUsername", username));
        Map<String,Object> map=new HashMap<>();
        map.put("message",list);
        return R.ok(map);
    }

}
